package com.yingli.fragment.viewpager;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.google.android.material.tabs.TabLayout;
import com.yingli.fragment.R;
import com.yingli.fragment.viewpager.fragment.ChatFragment;
import com.yingli.fragment.viewpager.fragment.ContactFragment;
import com.yingli.fragment.viewpager.fragment.FindFragment;
import com.yingli.fragment.viewpager.fragment.MineFragment;

import java.util.ArrayList;
import java.util.List;

public class ViewPagerActivity extends AppCompatActivity {

    //顶部的TabLayout的标题
    private String [] navsTitle = new String[]{"精选","发现","排行","分类"};
    //每个界面显示的视图
    private List<Fragment> fragments;
    private TabLayout tabLayout;
    private ViewPager viewPager;
    private ChatFragment chatFragment;
    private ContactFragment contactFragment;
    private FindFragment findFragment;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_viewpager);
        tabLayout = findViewById(R.id.tl_nav);
        viewPager = findViewById(R.id.view_pager);
        fragments = new ArrayList<>();
        chatFragment = new ChatFragment();
        fragments.add(chatFragment);
        contactFragment = new ContactFragment();
        fragments.add(contactFragment);
        findFragment = new FindFragment();
        fragments.add(findFragment);
        fragments.add(new MineFragment());
        viewPager.setAdapter(new TabAdapter(getSupportFragmentManager()));
        tabLayout.setupWithViewPager(viewPager);
        viewPager.setCurrentItem(2);
    }


    class Demo extends PagerAdapter{

        @Override
        public int getCount() {
            return 0;
        }

        @NonNull
        @Override
        public Object instantiateItem(@NonNull ViewGroup container, int position) {
            View view = LayoutInflater.from(ViewPagerActivity.this).inflate(R.layout.fragment_my,null);
            container.addView(view);
            return view;
        }

        @Override
        public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
            super.destroyItem(container, position, object);
        }

        @Override
        public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
            return false;
        }
    }

    class TabAdapter extends FragmentPagerAdapter {

        public TabAdapter(@NonNull FragmentManager fm) {
            super(fm);
        }

        //返回要显示页签的数量
        @Override
        public int getCount() {
            return fragments.size();
        }


        //返回每一项，即TabLayout的每一项的标题
        @Nullable
        @Override
        public CharSequence getPageTitle(int position) {
            return navsTitle[position];
        }

        @NonNull
        @Override
        public Fragment getItem(int position) {
            return fragments.get(position);
        }

    }

}
